package com.example.test_witns.adapter.blog;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.test_witns.R;
import com.example.test_witns.pojo.BlogCover;
import com.example.test_witns.utils.MyInfosHelper;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class BlogAdapter extends RecyclerView.Adapter<BlogAdapter.BlogViewHolder> {
    private static final Logger log = LoggerFactory.getLogger(BlogAdapter.class);
    private List<BlogCover> blogCoverList;
    private OnBlogCoverClickListener onBlogCoverClickListener;
    private OnMoreClickListener onMoreClickListener;
    private OnViewContentWhenHaveNotLoginListener onViewContentWhenHaveNotLoginListener;


    public interface OnBlogCoverClickListener{
        public void onBlogCoverClicked(long blogID, String authorName, String authorAvatarURL,
                                       String mainTitle, String subTitle, String introductionImageURL,
                                       String publishTime, String content, Integer likes_num, Integer stars_num,
                                       Integer comments_num, String blogContentImageURls);
    }

    public interface OnMoreClickListener{
        public void onMoreClicked();
    }

    public interface OnViewContentWhenHaveNotLoginListener{
        public void onViewContentWhenHaveNotLogin();
    }

    public BlogAdapter(List<BlogCover> blogCoverList){
        this.blogCoverList = blogCoverList;
    }

    public void setOnBlogCoverClickListener(OnBlogCoverClickListener onBlogCoverClickListener){
        this.onBlogCoverClickListener = onBlogCoverClickListener;
    }

    public void setOnMoreClickListener(OnMoreClickListener onMoreClickListener){
        this.onMoreClickListener = onMoreClickListener;
    }

    public void setOnViewContentWhenHaveNotLoginListener(OnViewContentWhenHaveNotLoginListener onViewContentWhenHaveNotLoginListener){
        this.onViewContentWhenHaveNotLoginListener = onViewContentWhenHaveNotLoginListener;
    }

    /**
     * @param parent   The ViewGroup into which the new View will be added after it is bound to
     *                 an adapter position.
     * @param viewType The view type of the new View.
     * @return
     */
    @NonNull
    @Override
    public BlogViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_blog_cover, parent, false);
        return new BlogAdapter.BlogViewHolder(view);
    }

    /**
     * @param holder   The ViewHolder which should be updated to represent the contents of the
     *                 item at the given position in the data set.
     * @param position The position of the item within the adapter's data set.
     */
    @Override
    public void onBindViewHolder(@NonNull BlogViewHolder holder, int position) {
        BlogCover blogCover = blogCoverList.get(position);
        holder.mainTitleTextView.setText(blogCover.getMainTitle());
        holder.subTitleTextView.setText(blogCover.getSubTitle());
        holder.tagTextView.setText(blogCover.getTag());
        holder.authorNameTextView.setText(blogCover.getAuthorName());
        holder.likeValueTextView.setText(String.valueOf(blogCover.getLikes_num()));
        holder.commentValueTextView.setText(String.valueOf(blogCover.getComments_num()));
        holder.starValueTextView.setText(String.valueOf(blogCover.getStars_num()));

        Glide.with(holder.itemView.getContext())
                .load(blogCover.getAuthorAvatarURL())
                .placeholder(R.drawable.blog_placeholder_image_foreground)  // 占位图
                .error(R.drawable.broken_image_foreground)             // 错误图
                .into(holder.publisherAvatarCircleImageView);

        if (blogCover.getIntroductionImageURL().equals("https://fftechuseravater.oss-cn-beijing.aliyuncs.com/images/blogCoverImages/null")){
            holder.introductionImageImageView.setImageResource(0);
            holder.blog_cover_hint.setText(blogCover.getMainTitle().substring(0, 3));
            holder.blog_cover_hint.setVisibility(View.VISIBLE);
        }else {
            Glide.with(holder.itemView.getContext())
                    .load(blogCover.getIntroductionImageURL())
                    .placeholder(R.drawable.blog_placeholder_image_foreground)  // 占位图
                    .error(R.drawable.broken_image_foreground)
                    .into(holder.introductionImageImageView);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyInfosHelper myInfosHelper = new MyInfosHelper(v.getContext());
                if (!myInfosHelper.checkIfLogin()){
                    onViewContentWhenHaveNotLoginListener.onViewContentWhenHaveNotLogin();
                }else {
                    onBlogCoverClickListener.onBlogCoverClicked(blogCover.getBlogID(), blogCover.getAuthorName(),
                            blogCover.getAuthorAvatarURL(), blogCover.getMainTitle(), blogCover.getSubTitle(),
                            blogCover.getIntroductionImageURL(), blogCover.getPublishTime(), blogCover.getContent(),
                            blogCover.getLikes_num(), blogCover.getStars_num(), blogCover.getComments_num(), blogCover.getBlogContentImageURLs());
                }
            }
        });
        holder.moreImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onMoreClickListener.onMoreClicked();
            }
        });

    }

    /**
     * @return
     */
    @Override
    public int getItemCount() {
        return blogCoverList.size();
    }

    public static class BlogViewHolder extends RecyclerView.ViewHolder{
        private CircleImageView publisherAvatarCircleImageView;
        private TextView mainTitleTextView;
        private TextView subTitleTextView;
        private ImageView introductionImageImageView;
        private ImageView moreImageView;
        private TextView tagTextView;
        private TextView authorNameTextView;
        private TextView likeValueTextView;
        private TextView commentValueTextView;
        private TextView starValueTextView;
        private TextView blog_cover_hint;

        public BlogViewHolder(@NonNull View itemView) {
            super(itemView);
            publisherAvatarCircleImageView = itemView.findViewById(R.id.blog_cover_author_avatar);
            mainTitleTextView = itemView.findViewById(R.id.blog_cover_main_title);
            subTitleTextView = itemView.findViewById(R.id.blog_cover_subtitle);
            introductionImageImageView = itemView.findViewById(R.id.blog_cover_introduction_image);
            moreImageView = itemView.findViewById(R.id.blog_cover_more);
            tagTextView = itemView.findViewById(R.id.blog_cover_tag);
            authorNameTextView = itemView.findViewById(R.id.blog_cover_author_name);
            likeValueTextView = itemView.findViewById(R.id.like_value);
            commentValueTextView = itemView.findViewById(R.id.comment_value);
            starValueTextView = itemView.findViewById(R.id.star_value);
            blog_cover_hint = itemView.findViewById(R.id.blog_cover_hint);
        }
    }

    public void addBlogCover(BlogCover blogCover){
        blogCoverList.add(blogCover);
        notifyItemInserted(blogCoverList.size() - 1);
    }

    public void removeAllBlogCover() {
        blogCoverList.clear();
        notifyDataSetChanged();
    }

    public void showCustomToast(View view, Context context, String message) {
        // 创建Toast
        View customView = LayoutInflater.from(context).inflate(R.layout.custom_toast_prohibited_layout, null);
        TextView toastText = customView.findViewById(R.id.toast_message);
        toastText.setText(message);

        Toast toast = new Toast(context);
        toast.setView(customView);
        toast.setDuration(Toast.LENGTH_SHORT);

        // 获取控件的位置（相对于屏幕）
        int[] location = new int[2];
        view.getLocationOnScreen(location); // 获取控件在屏幕上的位置
        int viewX = location[0]; // 控件的X坐标
        int viewY = location[1]; // 控件的Y坐标

        // 计算Toast的偏移量
        int xOffset = 0; // 水平偏移量，与控件的X坐标对齐
        int yOffset = viewY; // 垂直偏移量，让Toast显示在控件的顶部

        // 设置Toast的位置
        toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, xOffset, yOffset);

        // 显示Toast
        toast.show();
    }
}
